import axios from "axios";
import { getToken } from "./auth";
// import { Notify } from "vant";

const instance = axios.create({
  baseURL: "http://localhost:8888",
  //这个指的是接口域名，将来在使用的时候自动在接口前面加上http://localhost:8888
  timeout: 5000, //表示请求超时时间
  headers: {},
});

// 请求拦截
instance.interceptors.request.use(
  function (config) {
    if (getToken()) {
      config.headers.authorization = `Bearer ${getToken()}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);
// 响应拦截
instance.interceptors.response.use(
  function (response) {
    return response.data; //服务器返回响应
  },
  function (error) {
    const { response } = error; //错误返回的请求
    // console.log(response);
    const { status } = response; //错误请求的状态
    // console.log(status);
    // 如果返回401，说明没有登录，jwt的token身份验证没有
    if (status == 401) {
      // Notify("请先登录");
      //跳转到登录页面
      // location.href = "#/login";
    }
    return Promise.reject(error);
  }
);
// 封装get请求  axios.get(url,{params:params})
export const get = (url, params) => instance.get(url, { params });
// 封装post请求  axios.post(url,{name:'张三'，age:20})
export const post = (url, data) => instance.post(url, data);
// 封装删除接口
export const dele = (url, params) => instance.delete(url, { params });
//封装修改接口
export const put = (url, params) => instance.put(url, { params });

export default instance;
